<template>
  <div class="art_container">
    <el-row :gutter="20">
      <el-col :span="18" :xs="24" :sm="17">
        <el-card>
          <div class="">
            <div class="banner_container_title">
              <h1>{{ article.title }}</h1>
              <div class="wzInf">
                <span>
                  <i class="el-icon-user-solid"></i
                  >{{ article.user.nickname }}</span
                >
                <span>
                  <i class="iconfont iconshijian"></i
                  >{{ article.create_time | dataFormat2 }}
                </span>
              </div>
            </div>

            <div class="blog_content">
              <div
                v-highlight
                :style="styles"
                class="article-content markdown-body"
                v-html="articleHtml"
                ref="article"
              ></div>

              <!-- <md-preview
            :content="article.content"
            :is-bg-show="true"
            class="article-content markdown-body"
          /> -->
            </div>
            <div class="essay_pre_next">
              <p v-if="article.lastArticle">
                上一篇:
                <router-link
                  :to="{ path: `/essaycon/${article.lastArticle.id}` }"
                >
                  <span>{{ article.lastArticle.title }}</span>
                </router-link>
              </p>

              <p v-if="article.nextArticle">
                下一篇:
                <router-link
                  :to="{ path: `/essaycon/${article.nextArticle.id}` }"
                  ><span>{{ article.nextArticle.title }}</span>
                </router-link>
              </p>
            </div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="6" :xs="24" :sm="7">
        <!-- <el-card class="right-container"> -->
        <el-card class="infocard">
          <div
            class="infopic"
            :style="{ backgroundImage: 'url(' + article.image + ') ' }"
          ></div>
          <div class="author-avatar">
            <div
              class="avatar-box"
              :style="{ backgroundImage: 'url(' + article.user.avatar + ') ' }"
            ></div>
          </div>
          <h3>{{ article.user.name }}</h3>
          <p>{{ article.user.login_province }}{{ article.user.login_city }}</p>
          <div class="essay_inf">
            <div class="essay_inf_item">
              <span>0</span>
              <p>like</p>
            </div>
            <div class="essay_inf_item">
              <span>0</span>
              <p>like</p>
            </div>
            <div class="essay_inf_item">
              <span>0</span>
              <p>like</p>
            </div>
          </div>
        </el-card>
        <el-card class="hot_news">
          <h2 class="h_title">最新推荐</h2>
          <div class="article_list">
            <ul v-for="item in recommentList" :key="item.id">
              <router-link :to="{ path: `/essaycon/${item.id}` }">
                <li>
                  <img :src="item.image" alt="" />
                  <div class="recommen.cntent">
                    <p class="recommen_article_title">{{ item.title }}</p>
                    <span class="recommen_article_createTime">{{
                      item.create_time | dataFormat2
                    }}</span>
                  </div>
                </li>
              </router-link>
            </ul>
          </div>
        </el-card>
        <!-- </el-card> -->
      </el-col>
    </el-row>
  </div>
</template>
<script>
// import MdPreview from "@/components/article/ArticleContent/index.vue";
import { getEssayContent, getRecomment } from "@/request/api";
import tocbot from "tocbot";
import marked from "marked";
import highlight from "highlight.js";
import "github-markdown-css/github-markdown.css";
import "highlight.js/styles/atom-one-dark.css";

marked.setOptions({
  renderer: new marked.Renderer(),
  gfm: true,
  breaks: true,
  headerPrefix: "header-nav",
  highlight(code) {
    return highlight.highlightAuto(code).value;
  },
});

export default {
  name: "",
  // components: { MdPreview },
  props: [""],
  inject: ["reload"],
  directives: {
    highlight(el) {
      const blocks = el.querySelectorAll("pre code");
      blocks.forEach((block) => {
        highlight.highlightBlock(block);
      });
    },
  },
  computed: {
    articleHtml() {
      const renderer = new marked.Renderer();
      let index = 0;
      renderer.heading = function (text, level) {
        return `<h${level} id="titleAnchor-${index++}">${text}</h${level}>`;
      };
      // this.content = this.article.content;
      return marked(this.article.content || "", { renderer });
    },
    styles() {
      return {
        padding:
          typeof this.padding === "number" ? `${this.padding}px` : this.padding,
      };
    },
  },
  data() {
    return {
      article: {},
      isBgShow: false,
      content: "",
      padding: "10px",
      recommentList: [],
    };
  },
  created() {
    this.getEssayInfomation();
    this.getRecomment();
  },
  mounted() {},
  methods: {
    async getRecomment() {
      const { data: res } = await getRecomment();
      this.recommentList = res.data;
    },
    // 获取博客详 情信息
    async getEssayInfomation() {
      const { data: res } = await getEssayContent({
        id: this.$route.params.essayId,
      });
      this.article = res.data;
      this.$nextTick(() => {
        tocbot.init({
          tocSelector: "#toc", //要把目录添加元素位置，支持选择器
          contentSelector: ".article-content", //获取html的元素
          headingSelector: "h1, h2, h3,h4", //要显示的id的目录
          hasInnerContainers: true,
          onClick: function (e) {
            e.preventDefault();
          },
        });
      });
    },
  },
};
</script>
<style lang='less' scoped>
.art_container {
  max-width: 1200px;
  margin: 75px auto;
}
.banner_container {
  width: 100%;
  height: 100%;
  // background: url("@/assets/image/c.jpg") no-repeat;
  filter: blur(3px);
}
.atr_banner {
  //   height: 250px;
  position: relative;
}
.banner_container_title {
  //   display: inline-table;
  background: #fff;
  padding: 20px;
  padding-bottom: 0;
  h1 {
    color: #001737;
    font-size: 24px;
    margin: 0 0 20px 0;
  }
}
.wzInf {
  font-size: 12px;
  line-height: 28px;
  color: #808595;
  margin-bottom: 20px;
  border-bottom: #dbdbdb 1px dashed;
  span {
    margin: 0 20px 0 0;
    i {
      font-size: 12px;
      margin-right: 5px;
    }
  }
}
.article-content {
  word-break: break-word;
  font-size: 14px;
  line-height: 2;
}
.markdown-body p {
  text-align: justify;
}
.right-container {
  //   padding: 20px 24px;
  font-size: 14px;
}
.right-title {
  display: flex;
  align-items: center;
  line-height: 2;
  font-size: 16.8px;
  margin-bottom: 6px;
  font-weight: bold;
}

.right-title i {
  font-weight: bold;
}
/deep/ .toc-list-item {
  line-height: 2;
  list-style: none;
}
/deep/ .toc-link {
  display: block;
  border-left: 3px solid transparent;
  padding-left: 6px;
  color: #666261 !important;
  transition: all 0.2s ease-in-out;
}
/deep/ .is-active-link {
  border-left-color: #009d92;
  background: #00c4b6;
  color: #fff !important;
}
/deep/ .el-col-6 {
  height: 1600px;
}
.right-container /deep/ ol {
  padding-left: 0px !important;
}
.right-container /deep/.is-collapsible {
  padding-left: 8px !important;
}
.infocard {
  margin-bottom: 20px;
  text-align: center;
  background: #fff;
}
.infopic {
  background-color: #c5c8ce;
  background-position: 50%;
  background-repeat: no-repeat;
  background-size: cover;
  padding-top: 61.8%;
}
.author-avatar {
  border: 5px solid hsla(0, 0%, 92%, 0.5);
  border-radius: 100%;
  box-sizing: content-box;
  margin: -19% auto 0;
  padding-top: 38.2%;
  position: relative;
  width: 38.2%;
}
.avatar-box {
  background-position: 50%;
  background-repeat: no-repeat;
  background-size: cover;
  border-radius: 100%;
  height: 100%;
  left: 0;
  position: absolute;
  top: 0;
  width: 100%;
}
.infocard h3 {
  color: #282828;
  font-weight: 400;
  margin-top: 15px;
}
.infocard p {
  color: #808695;
  margin-top: 5px;
}
.essay_inf {
  display: flex;
  margin-top: 20px;
}
.essay_inf_item {
  flex: 1;
  padding-bottom: 20px;
}
.essay_inf_item span {
  color: #282828;
  font-size: 20px;
}
.essay_inf_item p {
  color: #808695;
}
.hot_news {
  border-radius: 3px;
  background: #fff;
  padding: 5px 20px 0;
  //   margin: 20px 0;
  overflow: hidden;
  position: relative;
}
.h_title {
  line-height: 50px;
  font-size: 22px;
}
.hot_news ul li {
  height: 80px;
  display: flex;
  align-items: center;
}
.hot_news ul li img {
  width: 60px;
  height: 60px;
  margin-right: 10px;
}
.recommen_article_createTime {
  color: rgb(73, 72, 72);
  font-size: 12px;
}
.essay_pre_next {
  line-height: 34px;
  // margin-bottom: 20px;
  overflow: hidden;
  display: flex;
  justify-content: space-between;
  margin: 10px 10px 20px 10px;
}
.essay_pre_next p {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.essay_pre_next a {
  margin-left: 10px;
  font-weight: bold;

  color: #001737;
}
</style>